<template>
  <div class="dashboard-container">
    <!-- <div v-loading="true" style="width:300px;height:300px;background-color:blue;" /> -->
    <div class="app-container">
      <page-tools show-before>
        <template #before>
          <span>共16条记录</span>
        </template>
        <template #after>
          <!-- 通过api -->
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <!-- 通过组件,通过rl -->
          <!-- <router-link to="/import">
            <el-button size="small" type="warning">导入</el-button>
          </router-link> -->
          <el-button size="small" type="danger">导出</el-button>
          <el-button size="small" type="primary" @click="showDialog = true">新增员工</el-button>
        </template>
      </page-tools>
      <el-table
        v-loading="loading"
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column
          type="index"
          width="100"
          align="center"
          label="序号"
          sortable
        />
        <el-table-column
          prop="username"
          align="center"
          label="姓名"
          sortable
        />
        <el-table-column
          prop="workNumber"
          align="center"
          label="工号"
          sortable
        />
        <el-table-column
          prop="formOfEmployment"
          align="center"
          label="聘用形式"
          :formatter="formatEmployment"
          sortable
        >
          <!-- 格式化列表-方式1.通过过滤器 -->
          <!-- <template v-slot="{row}">
            {{ row.formOfEmployment | hireType }}
          </template> -->
        </el-table-column>
        <el-table-column
          prop="departmentName"
          align="center"
          label="部门"
          sortable
        />
        <el-table-column
          prop="timeOfEntry"
          align="center"
          label="入职时间"
          sortable
        >
          <template v-slot="{row}">
            {{ row.timeOfEntry | formatDate }}
          </template>
        </el-table-column>
        <el-table-column
          prop="enableState"
          align="center"
          label="账户状态"
          sortable
        >
          <template v-slot="{row}">
            <!-- v-model="row.enableState" -->
            <!-- @change="row.enableState = $event?1:0" -->
            <el-switch
              :value="row.enableState === 1"
            />
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template v-slot="{row}">
            <el-button type="text" size="small">查看</el-button>
            <el-button type="text" size="small">转正</el-button>
            <el-button type="text" size="small">调岗</el-button>
            <el-button type="text" size="small">离职</el-button>
            <el-button type="text" size="small">角色</el-button>
            <el-button type="text" size="small" @click="deleteEmployee(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="center" align="middle" style="height: 60px">
        <el-pagination
          layout="prev, pager, next"
          :page-size="page.size"
          :total="page.total"
          @current-change="handleCurrentChange"
        />
      </el-row>
    </div>
    <add-employee :show-dialog.sync="showDialog" />
  </div>
</template>

<script>
import { delEmployee, getEmployeeList } from '@/api/employee'
// 按需导入
import EmployeeEnum from '@/api/constant/employees.js'
import AddEmployee from './components/add-employee.vue'
export default {
  name: 'Employees',
  filters: {
    hireType(val) {
      return EmployeeEnum.hireType.find(t => t.id === val).value
    }
  },
  components: {
    AddEmployee
  },
  props: {},
  data() {
    return {
      showDialog: false,
      list: [],
      loading: true,
      page: {
        page: 1,
        size: 10,
        total: 0
      }
    }
  },
  created() {
    this.getEmployeeList()
  },
  mounted() {},
  methods: {
    deleteEmployee(id) {
      // 通过elmecon
      // C:\Users\xxx\.vscode\extensions\ss.element-ui-snippets-0.7.2\snippets
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async action => {
        await delEmployee(id)
        this.getEmployeeList()
        // 通过eleme
        this.$message({
          message: '删除成功',
          type: 'success',
          showClose: true,
          duration: 3000
        })
      }).catch(() => {

      })
    },
    formatEmployment(row, column, cellValue, index) {
      const f = EmployeeEnum.hireType.find(t => t.id === cellValue)
      return f ? f.value : '未知'
    },
    async getEmployeeList() {
      this.loading = true
      const { rows, total } = await getEmployeeList(this.page)
      this.list = rows
      this.page.total = total
      this.loading = false
    },
    handleCurrentChange(newPage) {
      this.page.page = newPage
      this.getEmployeeList()
    },
    emporData() {
      const headers = {
        '姓名': 'username',
        '手机号': 'mobile'
      }
      import('@/vendor/Export2Excel').then(excel => {
        excel.export_json_to_excel({
          header: Object.keys(headers), // 表头 必填
          data: this.list.map((item, idx) => {
            const arr = [(this.page.page - 1) * this.page.size + idx]
            for (const key in headers) {
              const field = headers[key]
              arr.push(item[field])
            }
            return arr
          }) // 具体数据 必填

        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
